package com.wzh.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.wzh.pojo.Contact;
import com.wzh.service.ContactService;
import com.wzh.service.serviceImpl.ContactServiceImpl;


public class MainView extends JFrame {

	ContactService contactService = new ContactServiceImpl();

	private String[][] allContact = null;

    // 控制是否允许更改
	private String nowStatus = "delete";
	private boolean isEdited = false;


	private Object[][] rowData = null;
	private String[] columnNames = { "编号", "姓名", "电话", "联系地址" };

	DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);

	private JTextField jtfName;
	private JTextField jtfTelephone;
	private JTextArea jtfAddress;

	private JButton jbtAddContact;
	private JButton jbtUpdateContact;
	private JButton jbtDeleteContact;
	private JButton jbtAbout;


	// 信息窗口按钮
	JButton jbtExecute = new JButton("确认");
	JButton jbtCancel = new JButton("取消");

	private int selectedId;

	// private static final long serialVersionUID = 1L;

	public void setStatus() {
		jtfName.setEditable(isEdited);
		jtfTelephone.setEditable(isEdited);
		jtfAddress.setEditable(isEdited);
		jbtExecute.setEnabled(isEdited);
		jbtCancel.setEnabled(isEdited);
		jbtAddContact.setEnabled(nowStatus == "add" || nowStatus == "delete");
		jbtUpdateContact.setEnabled(nowStatus == "update" || nowStatus == "delete");
		jbtDeleteContact.setEnabled(nowStatus == "delete");
		jbtAbout.setEnabled(nowStatus == "delete");
	}

	// 更新加载数据
	public void loadData() {
		setStatus();
		allContact = contactService.queryAllContact();
		tableModel.setRowCount(0);
		for (int i = 0; i < allContact.length; i++) {
			tableModel.addRow(allContact[i]);
		}
		jtfName.setText("");
		jtfTelephone.setText("");
		jtfAddress.setText("");
	}

    // 添加用户
	public void addContact() {
		if (jtfName.getText().equals("") || jtfTelephone.getText().equals("")) {
			JOptionPane.showMessageDialog(null, "输入信息不完整,不能完成添加,至少需要姓名和一个电话号码");
		} else {
			boolean status = contactService.addContact(new Contact(jtfName.getText(), jtfTelephone.getText(), jtfAddress.getText()));
			if (status) {
				JOptionPane.showMessageDialog(null, "添加成功!");
				isEdited = false;
				nowStatus = "delete";
				loadData();
			} else {
				JOptionPane.showMessageDialog(null, "添加失败,请重新输入");
			}
		}
	}

	// 更改用户
	public void updateContact() {
		if (jtfName.getText().equals("") || jtfTelephone.getText().equals("")) {
			JOptionPane.showMessageDialog(null, "输入信息不完整,请重新输入！");
		} else {
			int status = contactService.updateContact(selectedId, jtfName.getText(),jtfTelephone.getText(), jtfAddress.getText());
			isEdited = false;
			nowStatus = "delete";
			loadData();// 刷新数据
			if (status == 0) {
				JOptionPane.showMessageDialog(null, "修改成功！");
			}
		}
	}

	public MainView() {
		this.setTitle("个人通讯录");
		int windowWidth = this.getWidth();
		int windowHeight = this.getWidth();
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		this.setLocation(screenWidth / 2 - windowWidth / 2 - 325, screenHeight / 2 - windowHeight / 2 - 300);

		this.setSize(550, 395);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(null);
		this.setResizable(false);

		final JTable table = new JTable(tableModel) {
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		// 联系人列表面板
		JScrollPane jsp_contactors = new JScrollPane(table);
		jsp_contactors.setBounds(3, 3, 540, 200);
		jsp_contactors.setBorder(BorderFactory.createTitledBorder("联系人列表"));

		JLabel jlName = new JLabel("姓名:");
		JLabel jlTelephone = new JLabel("电话:");
		JLabel jlAddress = new JLabel("地址：");

		jtfName = new JTextField();
		jtfTelephone = new JTextField();
		jtfAddress = new JTextArea();
		jtfAddress.setBorder(BorderFactory.createLineBorder(Color.black));
		jtfAddress.setDisabledTextColor(Color.GRAY);



		// 操作面板按钮
		jbtAddContact = new JButton("添加联系人信息");
		jbtUpdateContact = new JButton("修改选中联系人信息");
		jbtDeleteContact = new JButton("删除选中联系人信息");
		jbtAbout = new JButton("关于本软件");


		jlName.setBounds(10, 15, 35, 30);
		jtfName.setBounds(50, 20, 185, 25);
		jlTelephone.setBounds(10, 50, 45, 30);
		jtfTelephone.setBounds(50, 55, 185, 25);
		jlAddress.setBounds(10, 85, 45, 30);
		jtfAddress.setBounds(50, 90, 265, 61);
		jbtExecute.setBounds(245, 20, 70, 25);
		jbtCancel.setBounds(245, 55, 70, 25);

		jbtAddContact.setBounds(10, 20, 180, 25);
		jbtUpdateContact.setBounds(10, 55, 180, 25);
		jbtDeleteContact.setBounds(10, 90, 180, 25);
		jbtAbout.setBounds(10, 125, 180, 25);

		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent event) {
				if (event.getButton() == java.awt.event.MouseEvent.BUTTON1) {
					int focusedRowIndex = table.rowAtPoint(event.getPoint());
					if (focusedRowIndex == -1) {
						return;
					}
					jtfName.setText(table.getValueAt(focusedRowIndex, 1).toString());

					String strTel1 = (String) table.getValueAt(focusedRowIndex, 2);
					String strTel2 = (String) table.getValueAt(focusedRowIndex, 3);

					selectedId = Integer.parseInt((table.getValueAt(focusedRowIndex, 0)).toString());
					selectedId = Integer.parseInt(allContact[selectedId-1][4]);

					jtfTelephone.setText(strTel1);
					jtfAddress.setText(strTel2);
				}
			}
		});
		// 更改用户
		jbtUpdateContact.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				isEdited = true;
				nowStatus = "update";
				setStatus();
//				if (jtfName.getText().equals("") || jtfTelephone.getText().equals("")) {
//					JOptionPane.showMessageDialog(null, "输入信息不完整,请重新输入！");
//				} else {
//					int status = contactService.updateContact(selectedId, jtfName.getText(),jtfTelephone.getText(), jtfAddress.getText());
//					loadData();// 刷新数据
//					jtfName.setText("");
//					jtfTelephone.setText("");
//					jtfAddress.setText("");
//					if (status == 0) {
//						JOptionPane.showMessageDialog(null, "修改成功！");
//					}
//				}
			}
		});
		// 添加用户
		jbtAddContact.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				isEdited = true;
				nowStatus = "add";
				loadData();
//				if (jtfName.getText().equals("") || jtfTelephone.getText().equals("")) {
//					JOptionPane.showMessageDialog(null, "输入信息不完整,不能完成添加,至少需要姓名和一个电话号码");
//				} else {
//					boolean status = contactService.addContact(new Contact(jtfName.getText(), jtfTelephone.getText(), jtfAddress.getText()));
//					if (status) {
//						JOptionPane.showMessageDialog(null, "添加成功!");
//						loadData();
//						jtfName.setText("");
//						jtfTelephone.setText("");
//						jtfAddress.setText("");
//					} else {
//						JOptionPane.showMessageDialog(null, "添加失败,请重新输入");
//					}
//				}
			}
		});
		// 删除用户
		jbtDeleteContact.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int status = contactService.deleteById(selectedId);
				if(status != -1) {
					JOptionPane.showMessageDialog(null, "删除成功!");
					loadData();
				} else {
					JOptionPane.showMessageDialog(null, "删除异常！");
				}
			}
		});

		// 确定
		jbtExecute.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(nowStatus.equals("add")) {
					addContact();
				} else if(nowStatus.equals("update")) {
					updateContact();
				}
			}
		});
		// 取消
		jbtCancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				isEdited = false;
				nowStatus = "delete";
				loadData();
			}
		});

		// 操作面板
		JPanel jp_operation = new JPanel();
		jp_operation.setLayout(null);
		jp_operation.setBorder(BorderFactory.createTitledBorder("操作面板"));
		jp_operation.setBounds(340, 200, 200, 161);
		jp_operation.add(jbtAddContact);
		jp_operation.add(jbtUpdateContact);
		jp_operation.add(jbtDeleteContact);
		jp_operation.add(jbtAbout);

		// 信息窗口
		JPanel jp_info = new JPanel();
		jp_info.setLayout(null);
		jp_info.setBorder(BorderFactory.createTitledBorder("信息窗口"));
		jp_info.setBounds(3, 200, 335, 161);
		jp_info.add(jlName);
		jp_info.add(jtfName);
		jp_info.add(jlTelephone);
		jp_info.add(jtfTelephone);
		jp_info.add(jlAddress);
		jp_info.add(jtfAddress);
		jp_info.add(jbtExecute);
		jp_info.add(jbtCancel);



		this.getContentPane().add(jsp_contactors);
		this.getContentPane().add(jp_info);
		this.getContentPane().add(jp_operation);
		this.setVisible(true);
		loadData();
	}
}
